//
//  DLProgressImageView.m
//  XZDemo
//
//  Created by RookieYX on 16/7/12.
//  Copyright © 2016年 YXRookie. All rights reserved.
//

#import "DLProgressImageView.h"

@interface DLProgressImageView ()

@property (nonatomic, strong) CAShapeLayer *DLPbacklayer;

@property (nonatomic, strong) UIBezierPath *DLPbackpath;

@property (nonatomic, strong) CAShapeLayer *DLPfrontlayer;

@property (nonatomic, strong) NSTimer *DLTimer;

@property (nonatomic, assign) CGFloat DLProgress;

@end

@implementation DLProgressImageView

+ (instancetype)DLProgressImageView:(UIView *)superView
{
    return [[self alloc] initDLProgressImageView:superView];
}

- (instancetype)initDLProgressImageView:(UIView *)superView
{
    if (self = [super init]) {
        [superView addSubview:self];
        self.userInteractionEnabled = YES;
    }
    return self;
}
#pragma mark ---- 图片点击手势
- (void)startDownLoad
{
        [self showDownLoadProgress];
}
- (void)cancelDownLoad
{
        [self dismiss];
}

- (void)showDownLoadProgress
{
    self.DLProgress = 0.3;
    [self.layer addSublayer:self.DLPbacklayer];
    [self.DLPbacklayer addSublayer:self.DLPfrontlayer];
    [self.DLTimer setFireDate:[NSDate date]];
}

- (void)dismiss
{
    [self.DLPbacklayer removeFromSuperlayer];
    self.DLProgress = 0.3;
    [self.DLTimer setFireDate:[NSDate distantFuture]];
}

#pragma mark ---- 更新进度
- (void)updateProgress:(CGFloat)progress
{
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width / 2.f, self.frame.size.height / 2.f) radius:self.frame.size.width / 6.f startAngle:(M_PI * 2) * (progress - 0.3) - M_PI_2 endAngle:(M_PI * 2) * progress - M_PI_2 clockwise:YES];
    self.DLPfrontlayer.path = path.CGPath;
}
#pragma mark ---- nstimer
- (void)progressCount
{
    [self updateProgress:self.progress];
}
#pragma mark ---- 进度增长的幅度
- (CGFloat)progress
{
    return _DLProgress+=0.02f;
}

#pragma mark ---- 懒加载
- (CAShapeLayer *)DLPbacklayer
{
    if (!_DLPbacklayer) {
        _DLPbacklayer = [CAShapeLayer layer];
        _DLPbacklayer.fillColor = nil;
        _DLPbacklayer.path = self.DLPbackpath.CGPath;
        _DLPbacklayer.strokeColor = [UIColor blackColor].CGColor;
        _DLPbacklayer.lineWidth = 5.f;
    }
    return _DLPbacklayer;
}

- (UIBezierPath *)DLPbackpath
{
    if (!_DLPbackpath) {
        _DLPbackpath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width / 2.f, self.frame.size.height / 2.f) radius:self.frame.size.width / 6.f startAngle:-M_PI_2 endAngle:M_PI * 2 clockwise:YES];
    }
    return _DLPbackpath;
}

- (CAShapeLayer *)DLPfrontlayer
{
    if (!_DLPfrontlayer) {
        _DLPfrontlayer = [CAShapeLayer layer];
        _DLPfrontlayer.fillColor = nil;
        _DLPfrontlayer.strokeColor = [UIColor whiteColor].CGColor;
        _DLPfrontlayer.lineWidth = 5.f;
        _DLPfrontlayer.lineCap = @"round";
    }
    return _DLPfrontlayer;
}

- (NSTimer *)DLTimer
{
    if (!_DLTimer) {
        _DLTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressCount) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_DLTimer forMode:NSDefaultRunLoopMode];
    }
    return _DLTimer;
}

@end
